<?php

namespace Hilaoyu\LaravelExtend\Services\Auth;


use Carbon\Carbon;
use Hilaoyu\LaravelExtend\Services\Auth\Traits\ModelWithLoginRefreshTrait;
use Hilaoyu\LaravelExtend\Model;
use Hilaoyu\Utils\UtilSupport;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Support\Facades\Cache;

abstract class AbstractModelAuthUsers extends Model implements
    AuthenticatableContract,
    AuthorizableContract
{
    use Authenticatable, Authorizable;

    protected $table = 'auth_users';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'account',
        'name',
        'mobile',
        'email',
        'picture',
        'sex',
        'date_of_birth',
        'address',
        'lang_code',
        'remember_token',
        'lock_to',
        'last_login_platform',
        'last_login_source',
        'last_login_ip',
        'last_login_time',
        'summary',
        'reset_password_code_id',
    ];

    protected $appends = [
        'status',
        'status_name',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'reset_password_code_id',
        'remember_token',
    ];


    public function getValidationRules()
    {
        $rules = [
            'account' => 'required|string|account|max:255|unique:' . static::getTableCon() . ',account,' . $this->getOriginalKey() . ',' . static::getPkName(),
            'name' => 'required|string|max:255',
            'email' => 'nullable|email|max:255|unique:' . static::getTableCon() . ',email,' . $this->getOriginalKey() . ',' . static::getPkName(),
            'mobile' => 'nullable|string|mobile|unique:' . static::getTableCon() . ',mobile,' . $this->getOriginalKey() . ',' . static::getPkName(),
        ];

        return $rules;
    }

    public function getValidationMessages()
    {
        return [
            'account.required'=>'账号必填',
            'account.unique'=>'账号已存在',
            'name.required'=>'姓名/昵称必填',
            'email.unique'=>'邮箱已存在',
            'mobile.unique'=>'手机号已存在',
        ];
    }


    public function getStatusAttribute($value)
    {
        $lock_to = $this->getAttribute('lock_to');

        return is_null($value) ? ($lock_to <= Carbon::now()->toDateTimeString()) : ($value == 1);
    }

    public function getStatusNameAttribute()
    {
        return $this->getAttribute('status') ? "启用" : "禁用" ;
    }
    public function getSexAttribute($value)
    {
        if(!$value){
            $value = '保密';
        }
        return $value ;
    }


    use ModelWithLoginRefreshTrait;

    protected function fieldNamePlatform(){
        return 'last_login_platform';
    }
    protected function fieldNameSource(){
        return 'last_login_source';
    }
    protected function fieldNameLoginTime(){
        return 'last_login_time';
    }
    protected function fieldNameClientIp(){
        return 'last_login_ip';
    }
    protected function fieldNameExpireTime(): string
    {
        return  '';
    }


    public function checkPassword(string $password){
        $password = trim($password);
        if(empty($password)){
            $this->SetError('密码为空');
            return false;
        }


        $hashedPass = $this->password;
        if (app('hash')->check($password, $hashedPass)) {
            return true;
        }

        return false;
    }



}
